package com.railway.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.railway.bean.User;
import com.railway.service.UserService;
import com.railway.service.impl.UserServiceImpl;

@WebServlet("/UserShowMessage.do")

public class UserShowMessageServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String message = req.getParameter("message");
		if(message == null) {
			message = "";
		}else {
			message = req.getParameter("message");
		}
		UserService us = new UserServiceImpl();
		User u = us.selectByUserName(message);
		//System.out.println(message+"message");
		//System.out.println(u+"u");
		 ObjectMapper om = new ObjectMapper(); 
		 resp.setCharacterEncoding("UTF-8");
		 //设置 后端给前端传回的编码格式为utf-8 
		 resp.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		 resp.getWriter().print(om.writeValueAsString(u));
	}
	
	
}
